package com.baiyinet.ui.share;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.baiyinet.R;
import com.baiyinet.util.ShareUtils;
import com.baiyinet.util.Utils;

import java.util.ArrayList;
import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

public class ShareLinkDialogActivity extends Activity implements View.OnClickListener{

    View viewBackground;

    ListView lvShareList;

    TextView tvCancel;

    LinearLayout llShareLayout;

    public static ArrayList shareParamsList;

    public static int[] iconIds = {
            R.drawable.ssdk_oks_classic_wechat,
            R.drawable.ssdk_oks_classic_wechatmoments,
            R.drawable.ssdk_oks_classic_qq,
            R.drawable.ssdk_oks_classic_qzone,
            R.drawable.ssdk_oks_classic_sinaweibo
    };

    public static String[] shareTexts = {
            "分享给微信好友",
            "分享到微信朋友圈",
            "分享给QQ好友",
            "分享到QQ空间",
            "分享到新浪微博"
    };

    public static String[] shareNames = {
            Wechat.NAME,
            WechatMoments.NAME,
            QQ.NAME,
            QZone.NAME,
            SinaWeibo.NAME
    };

    private TranslateAnimation animShow;
    private TranslateAnimation animHide;
    private ShareItemAdapter sAdapter;
    private Bundle bundle;
    private String title;
    private String content;
    private String shareUrl;
    private String iconUrl;
    private ClipboardManager clip;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        sAdapter = new ShareItemAdapter(this);
        initView();
        initData();
        initAnims();

        llShareLayout.setAnimation(animShow);


    }

    private void initView() {
        viewBackground = findViewById(R.id.share_background);

        lvShareList = (ListView) findViewById(R.id.share_list);

        tvCancel = (TextView) findViewById(R.id.share_cancel);

        llShareLayout = (LinearLayout) findViewById(R.id.share_layout);

    }
    /*
    *   bundle.putString("title",title);
        bundle.putString("content",content);
        bundle.putString("shareUrl",shareUrl);
        bundle.putString("iconUrl",iconUrl);
    *
    * */
    private void initData() {

        clip = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

        Intent intent = getIntent();

        if (intent.getExtras() != null) {
            bundle = intent.getBundleExtra("message");

            title = bundle.getString("title");
            content = bundle.getString("content");
            shareUrl = bundle.getString("shareUrl");
            iconUrl = bundle.getString("iconUrl");

        }
        viewBackground.setOnClickListener(this);

        tvCancel.setOnClickListener(this);

        lvShareList.setAdapter(sAdapter);

        shareParamsList = new ArrayList();

        shareParamsList.add(new Wechat.ShareParams());
        shareParamsList.add(new WechatMoments.ShareParams());
        shareParamsList.add(new QQ.ShareParams());
        shareParamsList.add(new QZone.ShareParams());
        shareParamsList.add(new SinaWeibo.ShareParams());

        lvShareList.setOnItemClickListener(shareOnItemClickListener);

    }

    private AdapterView.OnItemClickListener  shareOnItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            doShare(i);
        }
    };

    private void doShare(int i) {
        Platform.ShareParams shareParams = (Platform.ShareParams) shareParamsList.get(i);
        shareParams.setText(content);
        shareParams.setTitle(title);
        shareParams.setSite(title);
        shareParams.setSiteUrl(shareUrl);
        shareParams.setImageUrl(iconUrl);
        shareParams.setUrl(shareUrl);
        shareParams.setTitleUrl(shareUrl);
        shareParams.setShareType(Platform.SHARE_WEBPAGE);

        if (i == 1) {

            clip.setPrimaryClip(ClipData.newPlainText(null, content));

            Utils.UI.showToastLong("内容已复制到剪贴板,长按粘贴");
            ShareUtils.shareUrlToWeChatMoment(ShareLinkDialogActivity.this, title, shareUrl, iconUrl);
        }else {

            Platform platform = ShareSDK.getPlatform(shareNames[i]);

            platform.setPlatformActionListener(new PlatformActionListener() {
                @Override
                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                    Utils.UI.showToastShort("分享成功");
                }

                @Override
                public void onError(Platform platform, int i, Throwable throwable) {
                    Utils.UI.showToastShort("分享失败");
                }

                @Override
                public void onCancel(Platform platform, int i) {
                    Utils.UI.showToastShort("取消分享");
                }
            });
            platform.share(shareParams);
        }


    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.share_background:
            case R.id.share_cancel:
                llShareLayout.clearAnimation();
                llShareLayout.setAnimation(animHide);
                break;
        }
    }

    private void initAnims() {
        animShow = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 3,
                Animation.RELATIVE_TO_SELF, 0);
        animShow.setDuration(400);

        animHide = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 3);
        animHide.setFillAfter(true);
        animHide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                finish();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        animHide.setDuration(400);
    }


}
